package cn.tac.test.base.reference;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @author tac
 * @since 20/10/2018
 */
public class PhantomRef {
    public static void main(String[] args) throws InterruptedException {
        ReferenceQueue<Foo> queue = new ReferenceQueue<>();
        PhantomReference<Foo> phantom = new PhantomReference<>(new Foo(), queue);
        System.out.println(phantom.get());
        System.out.println(phantom.isEnqueued());
        System.out.println(queue.poll());
        System.gc();
        Thread.sleep(10);
        System.out.println(phantom.isEnqueued());
        System.out.println(queue.poll());
    }

    private static class Foo {

    }
}
